<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件 -->
      <el-card v-loading="loading" class="tree-card">
        <tree-tools :node-data="company" :is-root="true" @add-depts="handleAddDepts" />
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 用了一个行列布局 -->
          <!--
            主要用的是data字段，node字段仅作了解，双大括号语法会报错
            data 当前节点的数据
          -->
          <template #default="{data}">
            <!-- {{ data.name }} - {{ data.manager }} -->
            <tree-tools
              :node-data="data"
              @del-depts="getDepartments"
              @add-depts="handleAddDepts"
              @edit-depts="handleEditDepts"
            />
          </template>
        </el-tree>
      </el-card>

    </div>
    <!-- 弹窗 -->
    <add-dept
      ref="addDeptDialog"
      :show-dialog.sync="showDialog"
      :node-data="nodeData"
      :depts-list="deptsList"
      @add-depts="getDepartments"
    />
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { reqGetDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'
export default {
  components: { treeTools, AddDept },
  data() {
    return {
      loading: false, // 用来控制进度弹层的显示和隐藏
      showDialog: false,
      company: {},
      nodeData: {},
      departs: [],
      deptsList: [], // 列表式数据
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  created() {
    this.getDepartments()
    console.log(this.$router)
  },
  methods: {

    // 获取全部的部门组织列表
    async getDepartments() {
      this.loading = true
      const { data } = await reqGetDepartments()
      console.log(data)
      this.company = { name: data.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(data.depts, '')
      this.deptsList = data.depts
      console.log(data.depts)
      this.loading = false
    },
    // 关闭弹窗

    // 处理添加操作, 显示弹层
    handleAddDepts(nodeData) {
      // 显示弹层
      this.showDialog = true
      // 存储传过来的数据, 这个应该记录下来, 将来添加需要用到
      this.nodeData = nodeData
    },
    handleEditDepts(nodeData) {
      // 显示弹层
      this.showDialog = true
      // 存储传过来的数据, 这个应该记录下来, 将来添加需要用到
      this.nodeData = nodeData
      // 之后的回显需求会调用请求，不用nodeData
      // 更新数据
      this.$refs.addDeptDialog.getEmployeeSimple()
      console.log(this.$refs.addDeptDialog, 'kkk')
      // 在这里调用方法, 让子组件, 获取部门详情的数据
      this.$refs.addDeptDialog.getDepartDetail(nodeData.id)
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px ;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
